package controllers;

import beans.PageBean;
import beans.WebServiceBean;
import models.*;
import play.libs.Codec;
import play.libs.Files;
import play.mvc.Controller;
import play.mvc.Scope;
import play.mvc.With;
import utils.Config;
import utils.MessageUtil;
import utils.SessionUtils;
import utils.StringUtils;

import java.io.File;
import java.util.List;

/**
 * Created by xieyicheng on 2015/2/25.
 */
@With(value = AdminCheck.class)
public class Activities extends Controller{
    public static void index(int page) {
        long size = Recommend.count("type = ?",1);
        PageBean pagebean = PageBean.getInstance(page, size);
        List<Recommend> productrecommendedList = Recommend.find("type = ?",1).fetch(pagebean.getCurPage(),pagebean.getPerPage());
        render(productrecommendedList, pagebean);
        MessageUtil.cleanMsg(Scope.Flash.current());
    }
    public static void add() {
        render();
        MessageUtil.cleanMsg(Scope.Flash.current());

    }
    /**
     * 添加活动
     * @param photoId
     * @param name
     */
    public static void addActivity(String photoId,String name,String content){
        if(!StringUtils.isEmpty(photoId)&&!StringUtils.isEmpty(content)&&!StringUtils.isEmpty(name)){

            Recommend productrecommended = new Recommend();
            productrecommended.name = name;
            productrecommended.photo_id = photoId;
            productrecommended.type=1;
            productrecommended.content=content;
            productrecommended.save();
            MessageUtil.generateInfoMsg("发布成功");
            index(0);
            MessageUtil.cleanMsg(Scope.Flash.current());

        }
        else{
            MessageUtil.generateErrorMsg("发布失败");
            index(0);
            MessageUtil.cleanMsg(Scope.Flash.current());

        }
    }
    /**
     * 显示活动信息
     * @param id
     */
    public static void show(String id) {
        Recommend productrecommended = Recommend.findById(id);
        if (productrecommended!=null) {
          //  Article article = Article.findById(productrecommended.keyId);
            Photo photo = Photo.findById(productrecommended.photo_id);
            render(photo,productrecommended);
        } else {
            MessageUtil.generateErrorMsg("信息不存在");
            index(0);
            MessageUtil.cleanMsg(Scope.Flash.current());

        }
        render();
        MessageUtil.cleanMsg(Scope.Flash.current());
    }

    /**
     * 删除活动
     * @param id
     * @param type
     */
    public static void delete(String id,int type){
        Recommend productrecommended = Recommend.findById(id);
        if(productrecommended!=null){
            productrecommended.delete();
            MessageUtil.generateInfoMsg("删除成功");
            index(0);
            MessageUtil.cleanMsg(Scope.Flash.current());

        }
        else{
            MessageUtil.generateErrorMsg("删除失败");
            index(0);
            MessageUtil.cleanMsg(Scope.Flash.current());

        }

    }
    /**
     * 修改推荐的页面
     * @param id
     */
    public static void modify(String id){
        Recommend productrecommended = Recommend.findById(id);
        if (productrecommended!=null) {
            render(productrecommended);
        } else {
            MessageUtil.generateErrorMsg("不能修改");
            index(0);
            MessageUtil.cleanMsg(Scope.Flash.current());

        }
        render();
        MessageUtil.cleanMsg(Scope.Flash.current());

    }
    /**
     * 修改推荐的方法
     * @param keyId
     * @param photoId
     * @param type
     * @param name
     * @param id
     */
    public static void modifyRecommend(String keyId,String photoId,int type,String name,String id){
        if(!StringUtils.isEmpty(photoId)&&!StringUtils.isEmpty(keyId)&&
                (type==0||type==1)&&!StringUtils.isEmpty(name)&&!StringUtils.isEmpty(id)){
            Recommend productrecommended = Recommend.findById(id);
            if(productrecommended!=null){
                productrecommended.keyId = keyId;
                productrecommended.name = name;
                productrecommended.photo_id = photoId;
                productrecommended.save();
                MessageUtil.generateInfoMsg("修改成功");
                index(0);

            }
            else{
                MessageUtil.generateErrorMsg("修改失败");
                index(0);

            }
        }
        else{
            MessageUtil.generateErrorMsg("字段不能为空");
            index(0);

        }
    }
    public static void ad(int page) {
        long total = Advertisement.count();
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 8);
        List<Advertisement> adList = Advertisement.find("order by createTime desc")
                .fetch(pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data =adList;
       // renderJSON(webServiceBean);
        render(webServiceBean,pageBean);
    }
    /**
     *上传广告图片
     **/
    public static void uploadAdvertisementPhoto(File image)
    {
        User user = SessionUtils.getLoginUser();
        String imgName=image.getName();
        String unique = Codec.UUID().replace("-", "").toUpperCase();
        File storeFile=new File(Config.imagePath+unique+imgName);
        Files.copy(image, storeFile);
        Advertisement ad = new Advertisement();
        ad.url = Config.imagePath+unique+imgName;
        ad.save();
        ad(0);

    }

    //删除广告
    public static void deleteAdvertisement(String adId)
    {
        User user = SessionUtils.getLoginUser();
        Advertisement ad = Advertisement.findById(adId);
        if(ad == null)
        {
            WebServiceBean webServiceBean = WebServiceBean.getFailInstance();
            webServiceBean.data = "不存在该广告！";
            renderJSON(webServiceBean);
        }
        File file = new File(ad.url);
        if(file.exists())
        {
            file.delete();
        }
        ad.delete();
       ad(0);
    }

    /**
     * 模糊搜索
     * @param query
     */
    public static void ShowUser(int page,String query){
        long size;
        long num = User.count();
        if(query!=null){
            size = User.CountUserByQuery(query);
        }
        else{
            query = "";
            size = User.count();
        }
        PageBean pageBean = PageBean.getInstance(page, size);
        List<User> userList = User.findUserByQuery(query, pageBean);
        render(userList,pageBean,num);
//       renderJSON(userList);
    }
    /**
     * 查看用户信息
     * @param id
     */
    public static void showUserInfo(String id){
        User user = User.find("id = ?",id).first();
        Photo photo = Photo.find("id = ?",user.photoId).first();
        String url = null;
        if(photo!=null){
            url = photo.getPhoto();
        }
        render(user,url);
    }


}
